package com.jh3502.vivo.function.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.citylist.CityBean;
import com.jh3502.vivo.function.reimnew.ReimNewPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/11 10:10
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class PlaceChooseDialog extends BaseActivity {
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.submit)
    TextView submit;
    private String cityname;
    private String adcode;

    @Override
    public ReimNewPresenter initPresenter() {
        return null;
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_travel_num_choose;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("选择地点");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {
        List<CityBean> datas = new Gson().fromJson(getIntent().getStringExtra("place"),
                new TypeToken<List<CityBean>>() {
                }.getType());
        ListChoiceAdapter adapter = new ListChoiceAdapter(this, datas);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cityname = datas.get(position).getName();
                adcode = datas.get(position).getAdcode();
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }


    @OnClick(R.id.cancel)
    public void onCancelClicked() {
        onBackPressed();
    }

    @OnClick(R.id.submit)
    public void onSubmitClicked() {
        if (TextUtils.isEmpty(cityname)) {
            showShortToast("最少选择一项");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("cityname", cityname);
        intent.putExtra("adcode", adcode);
        intent.putExtra("position", getIntent().getExtras().getInt("position"));
        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }

    private class ListChoiceAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<CityBean> list;

        public ListChoiceAdapter(Context context, List<CityBean> list) {
            this.inflater = LayoutInflater.from(context);
            this.list = list;


        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ListChoiceAdapter.ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_list_choice, null);
                holder = new ListChoiceAdapter.ViewHolder();
                holder.place = convertView.findViewById(R.id.item_list_choice_name);
                holder.check = convertView.findViewById(R.id.item_list_choice_check);
                convertView.setTag(holder);
            } else {
                holder = (ListChoiceAdapter.ViewHolder) convertView.getTag();
            }
            boolean check = ((ListView) parent).isItemChecked(position);
            holder.check.setChecked(check);
            holder.place.setText(list.get(position).getName());
            return convertView;
        }

        private class ViewHolder {
            TextView place;
            CheckBox check;

        }
    }
}
